import { ref, shallowRef } from 'vue'
import { defineStore } from 'pinia'
export const useKnowledgeStore = defineStore(
  'knowledge',
  () => {
    // 缓存选择了知识点或者题目的数据
    const knowledgeIdsArr = ref('')
    function setKnowledgeIds(ids) {
      knowledgeIdsArr.value = JSON.stringify(ids)
    }

    // 当前选中的知识点树  主要是数据图谱和列表分析两组件用到
    const knowledgeTree = shallowRef(null)
    function setKnowledgeTree(tree) {
      knowledgeTree.value = tree
    }

    // 当前节点的所有父节点
    const allParentNode = ref([])
    // 节点查找map
    const nodeLookup = ref({})

    const studentSituationData = ref([])
    function setStudentSituationData(data) {
      studentSituationData.value = data
    }

    const selectedStudent = ref([])
    function setSelectedStudent(student) {
      selectedStudent.value = student
    }
    // 刷新操作
    window.addEventListener('beforeunload', () => {
      setSelectedStudent([])
    })
    // 重置
    function reset() {
      setSelectedStudent([])
      setKnowledgeTree(null)
      setKnowledgeIds([])
      setStudentSituationData([])
      allParentNode.value = []
      nodeLookup.value = {}
    }

    return {
      knowledgeIdsArr,
      setKnowledgeIds,
      knowledgeTree,
      setKnowledgeTree,
      allParentNode,
      nodeLookup,
      studentSituationData,
      setStudentSituationData,
      selectedStudent,
      setSelectedStudent,
      reset
    }
  },
  {
    persist: [
      {
        paths: ['knowledgeIdsArr', 'knowledgeTree', 'studentSituationData'],
        storage: localStorage,
        key: 'ai-learn-knowledge'
      },
      {
        //
        paths: ['selectedStudent'],
        storage: sessionStorage,
        key: 'ai-learn-selected-student'
      }
    ]
  }
)
